package com.xyh;



import feign.Client;
import feign.Request;
import feign.Response;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import javax.swing.text.html.parser.Entity;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.Collection;
import java.util.HashMap;

public class MyClient implements Client {

    public Response execute(Request request, Request.Options options) throws IOException {
        System.out.println("this is my client");
        try{
            //创建一个默认的客户端
            CloseableHttpClient httpClient= HttpClients.createDefault();
            //获取调用的http方法
            final String method=request.method();
            HttpRequestBase httpRequestBase=new HttpRequestBase() {
                @Override
                public String getMethod() {
                    return method;
                }
            };
            httpRequestBase.setURI(new URI(request.url()));
            HttpResponse httpResponse=httpClient.execute(httpRequestBase);
            byte[] body= EntityUtils.toByteArray(httpResponse.getEntity());
            Response response= Response.builder()
                    .body(body)
                    .headers(new HashMap<String, Collection<String>>())
                    .status(httpResponse.getStatusLine().getStatusCode())
                    .build();
            return response;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
